package com.nangle.managehost.manager;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;

import com.nangle.managehost.db.DBManager;
import com.nangle.managehost.db.SQLiteTemplate;
import com.nangle.managehost.entity.Patient;
import com.nangle.managehost.utils.StringUtil;

import java.util.List;

/**
 * Created by YFB on 2017/4/13.
 */

public class PatientManager {
    private static PatientManager patientManager = null;
    private static DBManager manager = null;
    private PatientManager(Context context) {
        manager = DBManager.getInstance(context,  "yjhcloud");
    }
    public static PatientManager getInstance(Context context) {

        if (patientManager == null) {
            patientManager = new PatientManager(context);
        }

        return patientManager;
    }
    public long savePatient(Patient patient){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        ContentValues contentValues = new ContentValues();

        contentValues.put("name",patient.getName());
        contentValues.put("sex", patient.getSex());
        contentValues.put("age", patient.getAge());

        contentValues.put("nursingGrade", patient.getNursingGrade());
        contentValues.put("diagnosis", patient.getDiagnosis());
        contentValues.put("diet", patient.getDiet());

        contentValues.put("admission",patient.getAdmission());
        contentValues.put("bed", patient.getBed());
        contentValues.put("ye", patient.isYe());

        contentValues.put("wen", patient.isWen());
        contentValues.put("shu", patient.isShu());

        contentValues.put("guan", patient.isGuan());
        contentValues.put("jia", patient.isJia());

        return st.insert("patient", contentValues);
    }
    public List<Patient> getPaitentList(){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        List<Patient> list = st.queryForList(new SQLiteTemplate.RowMapper<Patient>() {

            @Override
            public Patient mapRow(Cursor cursor, int index) {
                Patient notice = new Patient();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setName(cursor.getString(cursor
                        .getColumnIndex("name")));
                notice.setDiagnosis(cursor.getString(cursor.getColumnIndex("diagnosis")));
                notice.setDiet(cursor.getString(cursor
                        .getColumnIndex("diet")));
                notice.setAdmission(cursor.getString(cursor
                        .getColumnIndex("admission")));
                notice.setSex(cursor.getInt(cursor
                        .getColumnIndex("sex")));
                notice.setNursingGrade(cursor.getInt(cursor
                        .getColumnIndex("nursingGrade")));
                notice.setBed(cursor.getInt(cursor
                        .getColumnIndex("bed")));
                notice.setAge(cursor.getInt(cursor.getColumnIndex("age")));
                notice.setYe(cursor.getInt(cursor
                        .getColumnIndex("ye")));
                notice.setWen(cursor.getInt(cursor
                        .getColumnIndex("wen")));
                notice.setShu(cursor.getInt(cursor
                        .getColumnIndex("shu")));
                notice.setGuan(cursor.getInt(cursor
                        .getColumnIndex("guan")));
                notice.setJia(cursor.getInt(cursor
                        .getColumnIndex("jia")));
                return notice;
            }

        }, "select * from patient" , null);
        return list;
    }

    public Patient getPatient(String bednumber){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        return st.queryForObject(new SQLiteTemplate.RowMapper<Patient>() {

            @Override
            public Patient mapRow(Cursor cursor, int index) {
                Patient notice = new Patient();
                notice.setId(cursor.getString(cursor.getColumnIndex("_id")));
                notice.setName(cursor.getString(cursor
                        .getColumnIndex("name")));
                notice.setDiagnosis(cursor.getString(cursor.getColumnIndex("diagnosis")));
                notice.setDiet(cursor.getString(cursor
                        .getColumnIndex("diet")));
                notice.setAdmission(cursor.getString(cursor
                        .getColumnIndex("admission")));
                notice.setSex(cursor.getInt(cursor
                        .getColumnIndex("sex")));
                notice.setNursingGrade(cursor.getInt(cursor
                        .getColumnIndex("nursingGrade")));
                notice.setBed(cursor.getInt(cursor
                        .getColumnIndex("bed")));
                notice.setAge(cursor.getInt(cursor.getColumnIndex("age")));
                notice.setYe(cursor.getInt(cursor
                        .getColumnIndex("ye")));
                notice.setWen(cursor.getInt(cursor
                        .getColumnIndex("wen")));
                notice.setShu(cursor.getInt(cursor
                        .getColumnIndex("shu")));
                notice.setGuan(cursor.getInt(cursor
                        .getColumnIndex("guan")));
                notice.setJia(cursor.getInt(cursor
                        .getColumnIndex("jia")));
                return notice;
            }

        }, "select * from patient where bed=?", new String[] { bednumber });
    }
    public void updateStatus(String id, Integer status){
        SQLiteTemplate st = SQLiteTemplate.getInstance(manager, false);
        ContentValues contentValues = new ContentValues();
        contentValues.put("bed", status);
        st.updateById("patient", id, contentValues);
    }

}
